package com.lzb.service;

import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;

import org.joda.time.LocalDateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StopWatch;

import com.lzb.common.easyui.datagrid.DataGridJsonDTO;
import com.lzb.dao.mapper.MngMomentMapper;
import com.lzb.domain.MngMoment;
import com.lzb.model.MngMomentModel;

@Service(MngMomentService.BEAN_PACKAGE_NAME)
@Transactional(readOnly=true)
public class MngMomentService {

	public static final String BEAN_PACKAGE_NAME = "com.lzb.service.MngMomentService";
	
//	@Resource
//	private SqlSessionTemplate sqlSession;
	
	@Resource
	private MngMomentMapper mngMomentMapper;
	
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public String add(MngMomentModel mngMomentModel) {
		mngMomentModel.setCreateDate(LocalDateTime.now().toString("yyyyMMdd"));
		mngMomentModel.setCreateTime(LocalDateTime.now().toString("HHmmss"));
		mngMomentMapper.insert(mngMomentModel);
		return mngMomentModel.getUuid();
	}
	
	public DataGridJsonDTO query(MngMomentModel queryModel) {
		DataGridJsonDTO result = new DataGridJsonDTO();
		result.setRows(mngMomentMapper.getBeansByBeanForPager(queryModel));
		result.setTotal(mngMomentMapper.getCount(queryModel));
		return result;
	}
	
	public List<MngMomentModel> getMngMomentByUUIDs(String... uuids) {
		List<MngMoment> _mngMomentLst = mngMomentMapper.getBeansByUUIDs(uuids);
		List<MngMomentModel> result = new LinkedList<MngMomentModel>();
		if(null != _mngMomentLst && _mngMomentLst.size() != 0) {
			for(MngMoment _m : _mngMomentLst) {
				MngMomentModel _model = new MngMomentModel();
				BeanUtils.copyProperties(_m, _model);
				result.add(_model);
			}
		}
		return result;
	}
	
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void delete(String... uuids) {
		mngMomentMapper.deleteByUUIDs(uuids);
	}
	
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void batchAdd(List<MngMoment> mngMomentLst) {
		mngMomentMapper.batchAdd(mngMomentLst);
	}
}
